package weaver.interfaces.qz.job;

import com.engine.qz.service.MeritsService;
import com.engine.qz.service.impl.MeritsServiceImpl;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2025/8/4
 * @Description 生成员工指标明细定时任务
 * @Author AdminZm
 */
public class GenerateIndexDtJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    @Override
    public void execute() {
        baseBean.writeLog("GenerateIndexDtJob start.");
        int dayOfMonth = LocalDate.now().getDayOfMonth();
        YearMonth ny = YearMonth.now();
        YearMonth sy = ny.minusMonths(1);
        int lastDay = ny.lengthOfMonth();
        String endDate = ny + "-" + lastDay;
        String startDate = ny + "-01";
        if (dayOfMonth <= 5) {
            startDate = sy + "-01";
        }

        MeritsServiceImpl meritsServiceImpl = new MeritsServiceImpl();
        Map<String, Object> params = new HashMap<>();
        RecordSet rs = new RecordSet();
        String sql = "SELECT id FROM uf_khjhdy WHERE szlx = 0 AND jhksrq <= '" + endDate + "' AND jhjsrq >= '" + startDate + "'";
        rs.execute(sql);
        while (rs.next()) {
            String id = rs.getString("id");
            if (StringUtils.isEmpty(id)) {
                continue;
            }
            try {
                params = new HashMap<>();
                params.put("khjh", id);
                Map<String, Object> map = meritsServiceImpl.generateIndexDt(params);
                baseBean.writeLog("GenerateIndexDtJob dt result:" + id + "、" + map);
            } catch (Exception e) {
                baseBean.writeLog("GenerateIndexDtJob dt error:" + id + "、" + e);
            }
        }
        baseBean.writeLog("GenerateIndexDtJob end.");
    }
}
